<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\admin\controller\Permissions;
use \think\Db;
class About extends Permissions
{

    public function add()
    {
        
        if($this->request->isPost()) {
            $post = $this->request->post();
            // var_dump($post);exit;
            if(false == Db::name('company1')->where('class',$post['class'])->update($post)) {
                return $this->error('提交失败');
            } else {
                // addlog();
                return $this->success('提交成功','admin/about/index?class='.$post['class']);
            }
        }else{
            $company=Db::name('company1')->where('class',$this->request->param('class'))->find();
            
            switch ($this->request->param('class')) {
            case 'call':
                $company['index']='联系我们';
                break;
            case 'range':
                $company['index']='服务范围';
                break;
            case 'dec':
                $company['index']='关于我们';
                break;
            case 'hezuo':
                $company['index']='合作机会';
                break;
        }

            $this->assign("company",$company);
            return $this->fetch();
        }
    }

    public function index(){
        $company=Db::name('company1')->where('class',$this->request->param('class'))->find();
        switch ($this->request->param('class')) {
            case 'call':
                $company['index']='联系我们';
                break;
            case 'range':
                $company['index']='服务范围';
                break;
            case 'dec':
                $company['index']='关于我们';
                break;
            case 'hezuo':
                $company['index']='合作机会';
                break;
        }
            $this->assign("company",$company);
            return $this->fetch();
    }

    /**
     * 公司荣耀
     */
    public function honor(){
        $honor=Db::name("honor")->order("id","desc")->paginate(10,false,["query"=>Request()->post()]);
        $this->assign("honor",$honor);
        $this->assign("page",$honor->render());
        return $this->fetch();
    }

    /**
     * 荣耀添加
     */
    public function add_honor(){
        if(Request()->IsPost()){
        $post=$this->request->Post();
            $id=$this->request->Post("id");
            $old_img="";
            if($id!=''){
                $old_img=Db::name("honor")->where('id',$id)->value('img');
            }
              // 获取表单上传文件 例如上传了001.jpg
            $file = Request()->file("img");
            if(!empty($file)){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/honor');
                if($info){
                    $img =$info->getSaveName();
                    @unlink($_SERVER['DOCUMENT_ROOT']."lawyer/public/uploads/honor/".$old_img);
                }else{
                    echo $file->getError();
                }
            }else{
                $img=$old_img;
            }
            $data=[
                    'title'=>$post['title'],
                    'intro'=>$post['intro'],
                    'img'=>$img,
                    "create_time"=>time()
                ];
                // return $data;
            if($id==null){  
            if($img=='')     return $this->error("图片必须添加");
                $id=Db::name('honor')->insert($data);
                if($id==false) return $this->error('保存失败');   
            }else{
                $data1=[
                    'title'=>$post['title'],
                    'intro'=>$post['intro'],
                    'img'=>$img,
                ];
                
                $res = Db::name('honor')->where('id',$id)->update($data1);
                if($res==false) return $this->error('修改失败');     
            }
            return $this->success("保存成功",'admin/about/honor');
        }elseif(Request()->has("id")){
            $id=Request()->param("id");
            $honor=Db::name("honor")->where("id",$id)->find();
            $this->assign("honor",$honor);
            return $this->fetch();            
        }else{
            return $this->fetch();
        }
    }

    /**
     * 删除荣耀
     */
    public function delete(){
        $id=Request()->param("id");
        $res = Db::name("honor")->where("id",$id)->delete();
        if ($res) {
           return $this->success("删除成功");
        }else{
            return $this->error("删除失败");
        }
    }

}